﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ERPStore.Extensions;

namespace ERPStore.MockConnector
{
	public class SalesService : ERPStore.Services.SalesService
	{
		private List<ERPStore.Models.ISaleDocument> m_OrderList;

		public SalesService(ERPStore.Services.IAccountService accountService)
		{
			m_OrderList = new List<ERPStore.Models.ISaleDocument>();
			this.AccountService = accountService;
		}

		protected ERPStore.Services.IAccountService AccountService { get; private set; }

		#region ISalesService Members

		public override IList<Models.Conveyor> GetConveyorList(Models.OrderCart cart)
		{
			return AccountService.GetConveyorList().ToList();
		}

		public override ERPStore.Models.ISaleDocument CreateOrderFromCart(ERPStore.Models.User user, ERPStore.Models.OrderCart cart, string payementModeName)
		{
			var result = new ERPStore.Models.Order();
			result.AllowPartialDelivery = cart.AllowPartialDelivery;
			result.BillingAddress = cart.BillingAddress;
			result.Id = m_OrderList.Count + 1;
			result.Code = string.Format("CMD{0:000000}", result.Id);
			result.CreationDate = cart.CreationDate;
			result.CustomerDocumentReference = cart.CustomerDocumentReference;
			result.IsPresent = cart.IsPresent;
			result.Items = new Models.LazyList<Models.ISaleItem>(() =>
			{
				var orderItems = new List<ERPStore.Models.ISaleItem>();
				foreach (var cartItem in cart.Items)
				{
					var orderItem = new ERPStore.Models.OrderItem();
					orderItem.Designation = cartItem.Product.Title;
					orderItem.Discount = cartItem.Discount;
					orderItem.PackagingValue = cartItem.Product.Packaging.Value;
					orderItem.Product = cartItem.Product;
					orderItem.ProductCode = cartItem.Product.Code;
					orderItem.Quantity = cartItem.Quantity;
					orderItem.RecyclePrice = cartItem.RecyclePrice;
					orderItem.SalePrice = cartItem.SalePrice;
					orderItem.SaleUnitValue = cartItem.SaleUnitValue;
					orderItem.TaxRate = cartItem.SalePrice.TaxRate;
					orderItems.Add(orderItem);
				}
				return orderItems;
			});
			result.MessageForCustomer = cart.Message;
			result.PaymentModeName = cart.PaymentModeName;
			result.ShippingAddress = cart.DeliveryAddress;
			result.User = user;
			result.Vendor = null;

			m_OrderList.Add(result);

			return result;
		}

		public override void AddPaymentToOrder(ERPStore.Models.Order order, string paymentModeName, string transactionId, object message)
		{

		}

		public override ERPStore.Models.Order GetLastOrder(ERPStore.Models.User user)
		{
			return null;
		}

		public override ERPStore.Models.Order GetOrderByCode(string orderCode)
		{
			var doc = m_OrderList.SingleOrDefault(i => i.Code == orderCode);
			return doc as ERPStore.Models.Order;
		}

		public override ERPStore.Models.Price GetProductSalePrice(ERPStore.Models.Product product, ERPStore.Models.User user, int Quantity, out bool isCustomerPriceApplied)
		{
			isCustomerPriceApplied = false;
			return product.SalePrice;
		}

		#endregion
	}
}
